<?php

/**
 * Description of Application_Model_Extjs
 *
 * @author Jean Brito
 */
abstract class Application_Model_Extjs {

    static public function getStoreFields($model) {
        $store = '[';
        foreach ($model->getFields() as $ifield) {
            $field = 'Application_Model_Fields_' . $ifield['type'];
            $field = $field::getStoreField($ifield);
            $store.='{';
            $store.='name:\'' . $field['name'] . '\',';
            $store.='type:\'' . $field['extType'] . '\',';
            if (isset($field['default']))
                $store.='defaultValue:\'' . $field['default'] . '\',';
            $store.='},';
        }
        $store.=']';
        return $store;
    }

    static public function getGridColumns($model, $columns = null) {
        $fields = $model->getFields();
        if ($columns == null) {
            $columns = Array();
            foreach ($fields as $field) {
                $columns[] = $field['name'];
            }
        }
        $grid = '[';
        foreach ($columns as $fieldName) {
            $field = 'Application_Model_Fields_' . $fields[$fieldName]['type'];
            $field = $field::getGridColumn($fields[$fieldName]);
            $grid.='{';
            $grid.='header:\'' . $field['header'] . '\',';
            $grid.='dataIndex:\'' . $field['name'] . '\',';
            if (isset($field['extRenderer']))
                $grid.='renderer:function(value,style,record){' . $field['extRenderer'] . '},';
            if (isset($field['flex']))
                $grid.='flex:' . $field['flex'] . ',';
            else
                $grid.='flex:1,';
            $grid.='},';
        }
        $grid.=']';
        return $grid;
    }

    static public function getFormInputs($model, $inputs = null) {
        $fields = $model->getFields();
        if ($inputs == null) {
            $inputs = Array();
            foreach ($fields as $field) {
                $inputs[] = $field['name'];
            }
        }
        $form = '[';
        foreach ($inputs as $fieldName) {
            $field = 'Application_Model_Fields_' . $fields[$fieldName]['type'];
            $field = $field::getFormInput($fields[$fieldName]);
            if (isset($field['hidden']) ? !$field['hidden'] : true) {
                $form.='{';
                if (isset($field['extXtype']))
                    $form.='xtype: \'' . $field['extXtype'] . '\',';
                $form.='fieldLabel:\'' . $field['fieldLabel'] . '\',';
                $form.='name:\'' . $field['name'] . '\',';
                if (isset($field['required']))
                    $form.='allowBlank: false,';
                if (isset($field['default']))
                    $form.='value: \'' . $field['default'] . '\',';
                if (isset($field['extTransform']))
                    $form.='transform: \'' . $field['extTransform'] . '\',';
                if (isset($field['extForceSelection']))
                    $form.='forceSelection: ' . $field['extForceSelection'] . ',';
                if (isset($field['extQueryMode']))
                    $form.='queryMode: \'' . $field['extQueryMode'] . '\',';
                if (isset($field['extDisplayField']))
                    $form.='displayField: \'' . $field['extDisplayField'] . '\',';
                if (isset($field['extValueField']))
                    $form.='valueField: \'' . $field['extValueField'] . '\',';
                if (isset($field['extStore']))
                    $form.='store: ' . $field['extStore'] . ',';
                $form.='},';
            }
        }
        $form.=']';
        return $form;
    }

    static public function getView($model, $view) {
        $setup=$model->getView($view);
        $view = "{";
        $view.="title:'".(isset($setup['title'])?$setup['title']:'')."',";
        if($setup['xtype']=='Ext.grid.Panel'){
            $view.='columns:'.self::getGridColumns($model, isset($setup['columns'])?$setup['columns']:null).',';
            $view.="store: Ext.data.StoreManager.lookup('".substr(get_class ($model),18,strlen(get_class ($model)))."Store'),";
        }
        //$view.='xtype: \'' . $setup['view'] . '\',';
        //}
        $view.= "}";
        return $view;
    }

}
